package com.ebarlas.fractal.compile;

import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionList;

import com.ebarlas.fractal.util.ArrayStack;
import com.ebarlas.fractal.util.Complex;

public class Literal implements StackCommand {
    
    private final Complex value;
    
    public Literal(Complex value) {
        this.value = value;
    }

    public int consumed() {
        return 0;
    }
    
    public int produced() {
        return 1;
    }    

    public void interpret(ArrayStack<Complex> stack) {
        stack.push(value);
    }

    public void compileConstructor(ConstantPoolGen cp, InstructionList il, InstructionFactory factory) {}

    public void compileExecute(ConstantPoolGen cp, InstructionList il, InstructionFactory factory) {
        CompilerUtils.pushComplexLiteral(cp, il, factory, value);       
    }
    
    @Override
    public boolean equals(Object obj) {
        return obj != null 
                && obj instanceof Literal 
                && ((Literal)obj).value.equals(value);
    }

}
